var app = getApp();
var api = require('../../api.js');
var util = require('../../utils/util.js');
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({
  data: {
    curSel:-1,
    topupMoney:0
  },
  changeSel:function(e){
    let index = e.currentTarget.dataset.index;
    let list = this.data.list;
    this.setData({
      curSel:index,
      topupMoney: list[index]['pay_money']
    })
  },
  formSubmit: function(e) {
    if (this.data.curSel == -1){
      wx.showToast({
        title: '请选择充值金额',
        icon:"none"
      })
      return;
    }
    if (wx.getStorageSync('real_name')) {
      app.ajax(api.vip_recharge, {
        rd_session: wx.getStorageSync('rd_session'),
        pay_money: this.data.list[this.data.curSel]['pay_money'],
        present_money: this.data.list[this.data.curSel]['present_money'],
      }, (res) => {
        console.log(res)
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function (res) {
            wx.showModal({
              content: '支付成功 ',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/mine/index',
                  })
                }else{
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              }
            })
          },
          'fail': function (res) {
            wx.showModal({
              content: '支付失败 ',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              }
            })
          }
        })
      });
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  onShow: function() {
    var that = this
    // 获取信息
    var rd_session = wx.getStorageSync('rd_session');
    app.ajax(api.vip_recharge_list, {
      rd_session: wx.getStorageSync('rd_session')
    }, (res, err) => {
      that.setData({
        list: res.data.lists,
      })
    })
    app.ajax(api.vip_introduce, {
      // rd_session: wx.getStorageSync('rd_session')
    }, (res, err) => {
      console.log(res)
      WxParse.wxParse('article', 'html', res.data.lists.content, that, 5);
    })
  }
});